package main

import (
	"bufio"
	"context"
	"flag"
	"fmt"
	"net"
	"os"
	"os/signal"
	"syscall"
)

type client struct {
	msg chan string
	log chan string
	ctx context.Context
	cancel context.CancelFunc
}

func newClient() *client {
	cli := &client{
		msg: make(chan string, 10),
		log: make(chan string, 10),
	}
	cli.ctx, cli.cancel = context.WithCancel(context.Background())
	return cli
}

func (c *client) Close() {
	close(c.msg)
	close(c.log)
}

func clientCli(cli *client) {
	reader, err := bufio.NewReader(os.Stdin)
	for {
		select {
		case <-cli.ctx.Done():
			fmt.Println("Stop Client Cli")
			return
		default:

		}
	}
}

func startClient(svrAddr string, cli *client) {
	conn, err := net.Dial("tcp4", svrAddr)
	if err != nil {
		fmt.Println("Failed to Connect to Server", err)
		return
	}
	defer conn.Close()
	buf := make([]byte, 1024)
	for {
		select {
		case <- cli.ctx.Done():
			fmt.Println("Stop Client Socket")
			return
		default:

		}
	}
}

func main() {
	//var svrAddr string
	svrAddr := flag.String("server", "192.168.1.2:1234", "Set Server Address")
	flag.Parse()
	cli := newClient()
	defer cli.Close()
	SetupCloseHandler(cli)
	go startClient(*svrAddr, cli)
	clientCli(cli)
}

// SetupCloseHandler creates a 'listener' on a new goroutine which will notify the
// program if it receives an interrupt from the OS. We then handle this by calling
// our clean up procedure and exiting the program.
func SetupCloseHandler(cli *client) {
	c := make(chan os.Signal)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM)
	go func() {
		<-c
		fmt.Println("\r- Ctrl+C pressed in Terminal")
		cli.cancel()
		// kill all process
		//exec.Command("pkill -f packet-verify")
		os.Exit(0)
	}()
}
